import time

from conf.settings import get_logger
from db import read_data
from interface import check_login, login_user_dict, get_bank_id, verify_pay_pwd, verify_balance, \
    save_balance, get_check_username


def withdraw_money():
    login_user = login_user_dict['username']
    # 验证银行卡号
    flag, bank_id = get_bank_id(tag='old', username=login_user)
    if not flag:
        return bank_id
    # 验证交易密码
    flag_pay, msg = verify_pay_pwd()
    if not flag_pay:
        return msg
    flag_balance, balance = verify_balance()
    if not flag_balance:
        return balance
    flag_save, msg = save_balance(tag='minus', balance=balance, username=login_user)
    if not flag_save:
        return msg
    log = f"{login_user} 取款 {balance} 元 成功"
    withdraw_logger = get_logger(tag='user', username=login_user)
    withdraw_logger.info(log)
    return log


def transfer_money():
    login_user = login_user_dict['username']
    flag, check_name = get_check_username()
    if not flag:
        return check_name
    # 验证银行卡号
    flag_bank_id, bank_id = get_bank_id(tag='old', username=check_name)
    if not flag:
        return bank_id
    # 验证交易密码
    flag_pay, msg = verify_pay_pwd()
    if not flag_pay:
        return msg
    flag_balance, balance = verify_balance()
    if not flag_balance:
        return balance
    # 转帐方减少钱
    flag_withdraw, msg_withdraw = save_balance(tag='minus', balance=balance, username=login_user)
    if not flag_withdraw:
        return msg
    # 收款方加钱
    flag_payee, msg_payee = save_balance(tag='add', balance=balance, username=check_name)
    if not flag_payee:
        return msg_payee
    log = f"{login_user} 转账给 {check_name}  {balance} 元   成功！"
    withdraw_logger = get_logger(tag='user', username=login_user)
    withdraw_logger.info(log)
    payee_logger = get_logger(tag='user', username=check_name)
    payee_logger.info(log)
    return log


def recharge():
    login_user = login_user_dict['username']
    # 验证银行卡号
    flag, bank_id = get_bank_id(tag='old', username=login_user)
    if not flag:
        return bank_id
    flag_balance, balance = verify_balance(tag='recharge')
    if not flag_balance:
        return balance
    flag_save, msg = save_balance(tag='add', balance=balance, username=login_user)
    if not flag_save:
        return msg
    log = f"{login_user} 存款 {balance} 元 成功"
    recharge_logger = get_logger(tag='user', username=login_user)
    recharge_logger.info(log)
    return log


def check_log():
    login_user = login_user_dict['username']
    flag, data = read_data(tag='log', path=f"{login_user}_log.log")
    if not flag:
        return data
    print(f"当前登录用户 {login_user} 日志如下")
    print(data)
    log = f"{login_user} 查看日志成功"
    logger = get_logger(tag='user', username=login_user)
    logger.info(log)
    return log


def bank_info():
    login_user = login_user_dict['username']
    flag, data = read_data(tag='info', path=f"{login_user}_info.json")
    if not flag:
        return data
    print(f"当前登录用户 {login_user} 信息如下")
    print(f"""
    用户名 : {data[login_user]['username']}
    密码 : {data[login_user]['password']}
    银行卡号 : {data[login_user]['bank_id']}
    支付密码 : {data[login_user]['pay_pwd']}
    余额 : {data[login_user]['balance']}
                    敏感信息已作加密！
    """)
    log = f"{login_user} 查看信息 成功 ！"
    get_logger(tag='user', username=login_user).info(log)
    return log


def quit_system():
    return 'break'


func_menu = '''
========================ATM=========================
    1.取款            2.转账           3.充值           
    4.个人日志         5.个人信息       6.退出ATM                                    
======================欢迎使用=======================
'''
atm_func_dict = {
    1: withdraw_money,
    2: transfer_money,
    3: recharge,
    4: check_log,
    5: bank_info,
    6: quit_system
}


@check_login
def atm_main():
    while True:
        time.sleep(0.5)
        print(func_menu)
        choice_func = input("请输入功能ID:>>>").strip()
        choice_func = int(choice_func)
        func = atm_func_dict[choice_func]
        msg = func()
        if msg == 'break':
            time.sleep(0.5)
            break
        print(msg)
    return "ATM系统已退出"
